﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Infrastructure;
using ContosoUniversity.Models;

namespace ContosoUniversity.DAL
{
    public class SchoolContext : DbContext
    {
        private readonly static string CONNECTION_STRING = "DefaultConnection";

        public DbSet<Course> Courses { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Instructor> Instructors { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<OfficeAssignment> OfficeAssignments { get; set; }


        public SchoolContext()
            : base(CONNECTION_STRING)//不写这个  默认的就是SchoolContext
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约

            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            modelBuilder.Entity<Instructor>().HasOptional(p => p.OfficeAssignment).WithRequired(p=>p.Instructor);
            modelBuilder.Entity<Course>().HasMany(c => c.Instructors).WithMany(i => i.Courses)
                .Map(t => t.MapLeftKey("CourseID").MapRightKey("InstructorID").ToTable("CourseInstructor"));
            //modelBuilder.Entity<Department>().HasOptional(x=>x.Administrator);

            base.OnModelCreating(modelBuilder);
        }
    }
}